import fs from 'fs'
import serverConfig from "../config/server.config";
import ExpressApp from "../service/express/Express.app";

ExpressApp.get('/data/fetch', (req, res) => {
  const id = req.query?.id
  const data = id ? require(`../${serverConfig.app.exportUrl}/data/${id}.txt`) : '[]'

  res.send(JSON.parse(data))
});

ExpressApp.post('/data/save', (req, res) => {
  const {id, data} = req.body

  const fileName = `${serverConfig.app.exportUrl}/data/${id}.txt`
  fs.openSync(fileName, 'w+')
  fs.writeFileSync(fileName, JSON.stringify(data))

  res.send('success')
});
